home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Utilities / MView / gxu / common.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-30  |  7.4 KB  |  233 lines

  1. #pragma once
  2.  
  3. #ifndef __COMMON_H
  4. #define __COMMON_H
  5.  
  6. /*/////////////////////////////////////////////////////////////////////////////
  7. //
  8. //  Copyright (C) 1999 Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //  File: common.h
  11. //
  12. //
  13. /////////////////////////////////////////////////////////////////////////////*/
  14.  
  15. #include <wtypes.h>
  16. #include <basetsd.h>
  17.  
  18. #include "d3dx8dbg.h"
  19.  
  20. #ifndef FALSE
  21.     #define FALSE 0
  22. #endif
  23.  
  24. #ifndef TRUE
  25.     #define TRUE 1
  26. #endif
  27.  
  28. typedef signed char         INT8;
  29. typedef unsigned char       UINT8;
  30. typedef short               INT16;
  31. typedef unsigned short      UINT16;
  32. typedef int                 INT32;
  33. typedef unsigned int        UINT32;
  34. typedef __int64             INT64;
  35. typedef unsigned __int64    UINT64;
  36.  
  37. #define GXMEMALLOC(__nBytes__) HeapAlloc(GetProcessHeap(), 0, __nBytes__)
  38. #define GXMEMFREE(__p__) HeapFree(GetProcessHeap(), 0, __p__)
  39.  
  40. #ifndef MAKE_USERERROR
  41.     #define MAKE_USERERROR(code)    MAKE_HRESULT(1,FACILITY_ITF,code)
  42. #endif
  43. #ifndef E_NOTINITIALIZED
  44.     #define E_NOTINITIALIZED        MAKE_USERERROR(0xFFFC)
  45. #endif
  46. #ifndef E_ALREADYINITIALIZED
  47.     #define E_ALREADYINITIALIZED    MAKE_USERERROR(0xFFFB)
  48. #endif
  49. #ifndef E_NOTFOUND
  50.     #define E_NOTFOUND              MAKE_USERERROR(0xFFFA)
  51. #endif
  52. #ifndef E_INSUFFICIENTDATA
  53.     #define E_INSUFFICIENTDATA      MAKE_USERERROR(0xFFF9)
  54. #endif
  55.  
  56.  
  57. #if defined(DBG) || defined(DEBUG) || defined (_DEBUG)
  58.     #ifndef _DEBUG
  59.         #define _DEBUG
  60.     #endif
  61.     #ifndef DBG
  62.         #define DBG 1
  63.     #endif
  64. #endif
  65.  
  66. #if defined(WIN32) || defined(_WIN32)
  67.     #ifndef WIN32
  68.         #define WIN32
  69.     #endif
  70.     #ifndef _WIN32
  71.         #define _WIN32
  72.     #endif
  73. #endif // #if defined(WIN32) || defined(_WIN32)
  74.  
  75.  
  76. #define MACSTART do {
  77. #define MACEND } while (0)
  78.  
  79. #if defined(_WINDOWS) || defined(WIN32)
  80.     #ifndef _INC_WINDOWS
  81.         #ifndef WIN32_EXTRA_LEAN
  82.             #define WIN32_EXTRA_LEAN
  83.         #endif
  84.         #ifndef WIN32_LEAN_AND_MEAN
  85.             #define WIN32_LEAN_AND_MEAN
  86.         #endif
  87.         #include <windows.h>
  88.         #include <wtypes.h>
  89.     #endif
  90.  
  91.     #ifndef _WINBASE_
  92.         #include <winbase.h>
  93.     #endif
  94.     #define ISVALIDINTERFACE(__p)               ((__p) && !::IsBadReadPtr(__p, sizeof(*(__p))) && !::IsBadCodePtr((FARPROC) ((PDWORD)(__p))[0]))
  95.     #define ISVALIDREAD(__p)                    ((__p) && !::IsBadReadPtr(__p, sizeof(*(__p))))
  96.     #define ISVALIDWRITE(__p)                   ((__p) && !::IsBadWritePtr(__p, sizeof(*(__p))))
  97.     #define ISVALIDREADWRITE(__p)               ((__p) && !::IsBadReadPtr(__p, sizeof(*(__p))) && !::IsBadWritePtr(__p, sizeof(*(__p))))
  98.     #define ISRGVALIDREAD(__p, __cItems)        ((__p) && !::IsBadReadPtr(__p, (__cItems) * sizeof(*(__p))))
  99.     #define ISRGVALIDWRITE(__p, __cItems)       ((__p) && !::IsBadWritePtr(__p, (__cItems) * sizeof(*(__p))))
  100.     #define ISRGVALIDREADWRITE(__p, __cItems)   ((__p) && !::IsBadReadPtr(__p, (__cItems) * sizeof(*(__p))) && !::IsBadWritePtr(__p, (__cItems) * sizeof(*(__p))))
  101.  
  102.     #define ISVALIDWINDOW(__hwnd)               ((__hwnd) && ::IsWindow(__hwnd))
  103.     #define ISVHWND(__hwnd)                     ISVALIDWINDOW(__hwnd)
  104.  
  105.     #include <winerror.h>
  106. #else
  107.     #define ISVALIDINTERFACE(__p)               ((__p) != NULL)
  108.     #define ISVALIDREAD(__p)                    ((__p) != NULL)
  109.     #define ISVALIDWRITE(__p)                   ((__p) != NULL)
  110.     #define ISVALIDREADWRITE(__p)               ((__p) != NULL)
  111.     #define ISRGVALIDREAD(__p, __cItems)        ((__p) != NULL)
  112.     #define ISRGVALIDWRITE(__p, __cItems)       ((__p) != NULL)
  113.     #define ISRGVALIDREADWRITE(__p, __cItems)   ((__p) != NULL)
  114. #endif
  115.  
  116. #define ISVI(__p)                               ISVALIDINTERFACE(__p)
  117. #define ISVR(__p)                               ISVALIDREAD(__p)
  118. #define ISVW(__p)                               ISVALIDWRITE(__p)
  119. #define ISVRW(__p)                              ISVALIDREADWRITE(__p)
  120. #define ISRGVR(__p, __cItems)                   ISRGVALIDREAD(__p, __cItems)
  121. #define ISRGVW(__p, __cItems)                   ISRGVALIDWRITE(__p, __cItems)
  122. #define ISRGVRW(__p, __cItems)                  ISRGVALIDREADWRITE(__p, __cItems)
  123.  
  124.  
  125. // Macro: GXRELEASE
  126. //    Safe release for COM objects
  127. // ***this code should never change - there is stuff that relies on the pointer being
  128. //    set to NULL after being released
  129. #ifndef GXRELEASE
  130.     #define GXRELEASE(_p) MACSTART if ((_p) != NULL) {(_p)->Release(); (_p) = NULL;} MACEND
  131. #endif
  132. #ifndef GXULTRARELEASE
  133.     #define GXULTRARELEASE(_p) MACSTART if (ISVI(_p)) {(_p)->Release();} (_p) = NULL; MACEND
  134. #endif
  135.  
  136.  
  137. #ifndef GXCOPYI
  138.     #define GXCOPYI(_pDstI, _pSrcI) MACSTART if ((_pDstI) = (_pSrcI)) (_pDstI)->AddRef(); MACEND
  139. #endif
  140. #ifndef CHECK_HR
  141.     #define CHECK_HR(__hr) MACSTART if (FAILED(__hr)) goto e_Exit; MACEND
  142. #endif
  143. #ifndef CHECK_MEM
  144.     #define CHECK_MEM(__p) MACSTART if ((__p) == NULL) { hr = E_OUTOFMEMORY; goto e_Exit; } MACEND
  145. #endif
  146. #ifndef CHECK_OUTPTR
  147.     #define CHECK_OUTPTR(__p) MACSTART if (!ISVW(__p)) return E_POINTER; else *(__p) = NULL; MACEND
  148. #endif
  149.  
  150.  
  151. //
  152. // DEBUG STUFF
  153. //
  154.  
  155. #ifdef _DEBUG
  156.     #include <stdio.h>
  157.     #include <crtdbg.h>
  158.     inline void __cdecl
  159.         _GXTrace(const char *szFmt, ...)
  160.     {
  161.         char szMsgBuf[1024];
  162.         va_list alist;
  163.         va_start( alist, szFmt );
  164.         _vsnprintf(szMsgBuf, 1024 - 1, szFmt, alist );
  165.     #if defined(_WINDOWS) || defined(WIN32) || defined(_WIN32)
  166.         OutputDebugString(szMsgBuf);
  167.     #endif // #if defined(_WINDOWS) || defined(WIN32) || defined(_WIN32)
  168.         fprintf(stderr, "%s", szMsgBuf);
  169.         fflush(stderr);
  170.     }
  171.  
  172.     //#define GXASSERT(exp)       ((void) 0)
  173.     #define GXASSERT(exp)       D3DXASSERT(exp)
  174.     #define GXVERIFY(exp)       GXASSERT(exp)
  175.     #define GXDEBUG_ONLY(exp)   (exp)
  176.     #define GXTRACE             ::_GXTrace
  177. #else // #ifdef _DEBUG
  178.     inline void __cdecl _GXTrace(const char *szFmt, ...) {}
  179.     #define GXASSERT(exp)       ((void) 0)
  180.     #define GXVERIFY(exp)       ((void) (exp))
  181.     #define GXDEBUG_ONLY(exp)   ((void) 0)
  182.     #define GXTRACE             1 ? (void) 0 : ::_GXTrace
  183. #endif // #else // #ifdef _DEBUG
  184.  
  185.  
  186. #ifndef GXSETMAX
  187.     #define GXSETMAX(dst, src1, src2)   MACSTART if ((src1) < (src2)) (dst) = (src2); else (dst) = (src1); MACEND
  188.     #define GXSETMIN(dst, src1, src2)   MACSTART if ((src1) > (src2)) (dst) = (src2); else (dst) = (src1); MACEND
  189.     #define GXUPDATEMAX(dst, src)       MACSTART if ((src) > (dst)) (dst) = (src); MACEND
  190.     #define GXUPDATEMIN(dst, src)       MACSTART if ((src) < (dst)) (dst) = (src); MACEND
  191. #endif
  192.  
  193. #ifndef GXISWAP
  194.     #define GXISWAP(a,b)                MACSTART (a)^=(b); (b)^=(a); (a)^=(b); MACEND
  195. #endif
  196.  
  197. // Macro: GXPSWAP
  198. //    This macro swaps two pointers in place using 3 xor's.
  199. #ifndef GXPSWAP
  200. #define GXPSWAP(a,b,type) MACSTART \
  201.     a = (type *)(int(a) ^ int(b)); \
  202.     b = (type *)(int(a) ^ int(b)); \
  203.     a = (type *)(int(a) ^ int(b)); \
  204. MACEND
  205. #endif
  206.  
  207.  
  208. template<class T>
  209. inline void
  210. swap(T &a, T &b)
  211. {
  212.     T c(a);
  213.     a = b;
  214.     b = c;
  215. }
  216.  
  217. static const int x_rgmod3[6]={0,1,2,0,1,2};
  218. inline int MOD3(int j)
  219. {
  220.     GXASSERT(j>=0 && j<6);
  221.     return x_rgmod3[j];
  222. }
  223.  
  224.  
  225. // Pointer checking
  226. #if DBG
  227. inline BOOL DWORD_PTR_OK(DWORD *p) {return !IsBadWritePtr(p, sizeof(DWORD));}
  228. #else
  229. inline BOOL DWORD_PTR_OK(DWORD *p) {return (p? TRUE:FALSE);}
  230. #endif
  231.  
  232. #endif  // #ifndef __COMMON_H
  233.